package com.chanzany.observer.case1;

import java.util.LinkedList;
import java.util.List;

//具体主题，内部维护了一个观察者的集合
public class WeatherData implements Subject {
    private static final List<Observer> observers = new LinkedList<>();

    private float temperature;
    private float pressure;
    private float humidity;

    @Override
    public void register(Observer o) {
        observers.add(o);
    }

    @Override
    public void remove(Observer o) {
        observers.remove(o);
    }

    public void update(float temperature, float pressure, float humidity){
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        notifyObservers();
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature,pressure,humidity);
        }
    }
}
